/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import opiam.admin.faare.service.services.references.ReferenceList;


/**
 * This class conveys a reference list based on several objects.
 *
 */
public class JBSearchReferenceListDescriptor implements java.io.Serializable
{
    /** Reference list name. */
    private String name;

    /** Reference list scope. */
    private String scope = ReferenceList.SCOPE_SESSION;

    /** Search base dname. */
    private String dn;

    /** Attribute for labels of list elements. */
    private String labelattr;

    /** Attribute for values of list elements. */
    private String valueattr;

    /** Search filter. */
    private String filter;

    /** Sorting flag. */
    private String sort;

    /**
     * Returns the reference list name.
     * @return name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the reference list name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Checks whether the reference list is application scoped.
     *
     * @return true if it is, false if it is not.
     */
    public boolean isScopeApplication()
    {
        return (scope.compareToIgnoreCase(ReferenceList.SCOPE_APPLICATION) == 0);
    }

    /**
     * Checks whether the reference list is session scoped.
     *
     * @return true if it is, false if it is not.
     */
    public boolean isScopeSession()
    {
        return (scope.compareToIgnoreCase(ReferenceList.SCOPE_SESSION) == 0);
    }

    /**
     * Checks whether the reference list is request scoped.
     *
     * @return true if it is, false if it is not.
     */
    public boolean isScopeRequest()
    {
        return (scope.compareToIgnoreCase(ReferenceList.SCOPE_REQUEST) == 0);
    }

    /**
     * Returns the search base dname.
     * @return Search base dname
     */
    public String getDn()
    {
        return dn;
    }

    /**
     * Sets the search base dname.
     * @param adn The dname to set
     */
    public void setDn(String adn)
    {
        this.dn = adn;
    }

    /**
     * Displays the reference list.
     *
     * @return Reference list definition.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));
        buf.append("scope = ");
        buf.append(scope);
        buf.append(System.getProperty("line.separator"));
        buf.append("dn = ");
        buf.append(dn);
        buf.append(System.getProperty("line.separator"));
        buf.append("labelattr = ");
        buf.append(labelattr);
        buf.append(System.getProperty("line.separator"));
        buf.append("valueattr = ");
        buf.append(valueattr);
        buf.append(System.getProperty("line.separator"));
        buf.append("filter = ");
        buf.append(filter);
        buf.append(System.getProperty("line.separator"));
        buf.append("sort = ");
        buf.append(sort);
        buf.append(System.getProperty("line.separator"));

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the scope.
     * @return String
     */
    public String getScope()
    {
        return scope;
    }

    /**
     * Sets the scope.
     * @param ascope The scope to set
     */
    public void setScope(String ascope)
    {
        this.scope = ascope;
    }

    /**
     * Returns the search filter.
     * @return filter
     */
    public String getFilter()
    {
        return filter;
    }

    /**
     * Returns the list elements label attribute.
     * @return attribute name
     */
    public String getLabelattr()
    {
        return labelattr;
    }

    /**
     * Returns the list elements value attribute.
     * @return attribute name
     */
    public String getValueattr()
    {
        return valueattr;
    }

    /**
     * Sets the search filter.
     * @param afilter The filter to set
     */
    public void setFilter(String afilter)
    {
        this.filter = afilter;
    }

    /**
     * Sets the list elements label attribute.
     * @param alabelattr The attribute name to set
     */
    public void setLabelattr(String alabelattr)
    {
        this.labelattr = alabelattr;
    }

    /**
     * Sets the list elements value attribute.
     * @param avalueattr The attribute name to set
     */
    public void setValueattr(String avalueattr)
    {
        this.valueattr = avalueattr;
    }

    /**
     * Gets attributes used for labels and for values of list elements.
     *
     * @return Array of string with both attributes
     */
    public String[] getAttributes()
    {
        String[] attrs = new String[2];
        attrs[0] = labelattr;
        attrs[1] = valueattr;

        return attrs;
    }

    /**
     * Returns the sort flag.
     * @return "true" to sort, else "false".
     */
    public String getSort()
    {
        return sort;
    }

    /**
     * Sets the sort flag.
     * @param asort The flag to set : "true" to sort, else "false".
     */
    public void setSort(String asort)
    {
        this.sort = asort;
    }

    /**
     * Checks whether the list elements should be sorted.<br>
     * Sort should be performed on list elements labels.<br>
     * Default is yes.
     *
     * @return true is list elements should be sorted, false if they should not.
     */
    public boolean isToSort()
    {
        if (sort == null)
        {
            return true;
        }
        else
        {
            return (sort.compareToIgnoreCase("true") == 0);
        }
    }
}
